/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

/**
 *
 * @author THANH PHUC
 */
public class GameThread extends Thread{
    /**
    * A handle back to the graphical components.
    */
    private long myLastRefreshTime;
    private JumpCanvas myJumpCanvas;
    
    
  GameThread(JumpCanvas canvas) {
    myJumpCanvas = canvas;
  }
    private long getWaitTime() {
    long retVal = 1;
    long difference = System.currentTimeMillis() - myLastRefreshTime;
        if(difference < 50) {
          retVal = 50 - difference;
        }
    return(retVal);
    }

     void resumeGame() {
    //myShouldPause = false;
    synchronized(this) {
      notify();
    }
  }
    
    public void run() {
    // flush any keystrokes that occurred before the 
    // game started:
    myJumpCanvas.flushKeys();
    //myShouldStop = false;
    //myShouldPause = false;
    while(true) {
      myLastRefreshTime = System.currentTimeMillis();
      /*
      if(myShouldStop) {
	break;
      }*/
      /*
      while(myShouldPause) {
	synchronized(this) {
	  try {
	    wait();
	  } catch(Exception e) {}
	}
      }*/
      myJumpCanvas.checkKeys();
      myJumpCanvas.advance();
      // we do a very short pause to allow the other thread 
      // to update the information about which keys are pressed:
      synchronized(this) {
	try {
	  //wait(1);
	  wait(getWaitTime());
	} catch(Exception e) {}
      }
    }
  }

}
